﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SaleSoft.Models.ScreenClient;
using SaleSoft.Constants;
using SaleSoft.Helper;
using SaleSoft.Models;
using Telerik.Web.Mvc;

namespace SaleSoft.Areas.administrator.Controllers
{
    public class LoaiNoiDungWebController : Controller {
        #region Properties Section
        private SaleSoftEntities db = new SaleSoftEntities();
        #endregion

        #region Views Section
        public ActionResult Index()
        {
            LoaiNoiDungWebScreenClient screen = new LoaiNoiDungWebScreenClient();
            if (Session[ScreenClientConstants.LOAI_NOI_DUNG_WED] != null) {
                screen = (LoaiNoiDungWebScreenClient)Session[ScreenClientConstants.LOAI_NOI_DUNG_WED];
            }
            Search(screen);
            return View(screen);
        }

        [HttpPost]
        [GridAction]
        public ActionResult _Index ( LoaiNoiDungWebScreenClient screen ) {
            Search(screen);
            return View(new GridModel(screen.DSLoaiNoiDungWeb));
        }

        #endregion

        #region CRUD Section
        [HttpPost]
        [GridAction]
        public ActionResult _Insert () {
            SaleSoft.Models.BaseClient.LoaiNoiDungWeb loaiClient = new Models.BaseClient.LoaiNoiDungWeb();
            TryUpdateModel(loaiClient);
            loai_noidungweb loai = new loai_noidungweb();
            ObjectHelper.Copy(loaiClient, loai);
            db.loai_noidungweb.AddObject(loai);
            db.SaveChanges();

            LoaiNoiDungWebScreenClient screen = new LoaiNoiDungWebScreenClient();
            if (Session[ScreenClientConstants.LOAI_NOI_DUNG_WED] != null) {
                screen = (LoaiNoiDungWebScreenClient)Session[ScreenClientConstants.LOAI_NOI_DUNG_WED];
            }
            Search(screen);
            return View(new GridModel(screen.DSLoaiNoiDungWeb));
        }
        [HttpPost]
        [GridAction]
        public ActionResult _Edit (int id) {
            loai_noidungweb loai = db.loai_noidungweb.Single(p => p.id_loainoidung == id);
            SaleSoft.Models.BaseClient.LoaiNoiDungWeb loaiClient = new Models.BaseClient.LoaiNoiDungWeb();
            TryUpdateModel(loaiClient);
            loaiClient.id_loainoidung = id;
            ObjectHelper.Copy(loaiClient, loai);

            db.ObjectStateManager.ChangeObjectState(loai, System.Data.EntityState.Modified);
            db.SaveChanges();

            LoaiNoiDungWebScreenClient screen = new LoaiNoiDungWebScreenClient();
            if (Session[ScreenClientConstants.LOAI_NOI_DUNG_WED] != null) {
                screen = (LoaiNoiDungWebScreenClient)Session[ScreenClientConstants.LOAI_NOI_DUNG_WED];
            }
            Search(screen);
            return View(new GridModel(screen.DSLoaiNoiDungWeb));
        }

        [HttpPost]
        [GridAction]
        public ActionResult _Delete ( int id ) {
            loai_noidungweb loai = db.loai_noidungweb.Single(p => p.id_loainoidung == id);
            try {
                db.loai_noidungweb.DeleteObject(loai);
                db.SaveChanges();
            } catch {
            }

            LoaiNoiDungWebScreenClient screen = new LoaiNoiDungWebScreenClient();
            if (Session[ScreenClientConstants.LOAI_NOI_DUNG_WED] != null) {
                screen = (LoaiNoiDungWebScreenClient)Session[ScreenClientConstants.LOAI_NOI_DUNG_WED];
            }
            Search(screen);
            return View(new GridModel(screen.DSLoaiNoiDungWeb));
        }

        #endregion

        #region Helper Section
        private void Search ( LoaiNoiDungWebScreenClient screen ) {
            screen.DSLoaiNoiDungWeb.Clear();

            var data = db.loai_noidungweb.OrderBy(p => p.tenloai);
            foreach (loai_noidungweb loai in data) {
                SaleSoft.Models.BaseClient.LoaiNoiDungWeb loaiClient = new Models.BaseClient.LoaiNoiDungWeb();
                ObjectHelper.Copy(loai, loaiClient);
                loaiClient.Deleteable = loai.ds_noidungweb.Count == 0;
                screen.DSLoaiNoiDungWeb.Add(loaiClient);
            }
            Session[ScreenClientConstants.LOAI_NOI_DUNG_WED] = screen;
        }
        #endregion
    }
}
